﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PayloadTracker.Tiles
{
    [Serializable]
    public abstract class ITileProvider : System.Xml.Serialization.IXmlSerializable
    {
        public abstract class TileKey
        {
            public abstract string GetHash();
        }

        public bool Enabled {get;set;}
        public abstract string Name();

        public abstract List<TileKey> GetUniqueKeysForRegion(Coordinate UL, Coordinate LR, int Zoom);
        public abstract Tile GetTileFromKey(TileKey key, bool Async);


        public virtual List<Tile> GetTilesForRegion(Coordinate UL, Coordinate LR, int Zoom, bool Async)
        {
            List<Tile> ret = new List<Tile>();

            foreach (var key in GetUniqueKeysForRegion(UL, LR, Zoom))
                ret.Add(GetTileFromKey(key, Async));

            return ret;
        }

        public string GetCachePath()
        {
            string tilecache = Settings.Default["TileCache"] as string;
            string path = System.IO.Path.Combine(tilecache, Name());
            
            if (!System.IO.Directory.Exists(tilecache))
                System.IO.Directory.CreateDirectory(tilecache);
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);
           
            return path;
        }



        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public abstract void ReadXml(System.Xml.XmlReader reader);

        public abstract void WriteXml(System.Xml.XmlWriter writer);

        #endregion

    }
}
